﻿
function main_options( block_id ) {
    this.vdiv = block_id;
    this.is = false;
}

main_options.prototype.show = function() 
{ 
    var a = this;
    if( !a.is ) { a.create(); a.is = true; } 
    else $('#'+a.vdiv).show();
};

main_options.prototype.hide = function() 
{ 
    $('#'+this.vdiv).hide();
};
    
main_options.prototype.create = function() 
{
var dlgID = this.vdiv;
var optdlg = $('#'+dlgID);
var newdlg = $('#'+dlgID+' div') ? true : false;
var neighbouring;
var header_test;
var use_default_lists;
var default_action;
var spam_house;
var reject_message;
var submit_button;
var data;

    if( newdlg ) {
        optdlg.html(
'<div class="ui-widget-header ui-corner-all"><span>Опции Анти-Спам фильтра для домена</span></div>'+
'<div class="ui-widget-content  ui-corner-all" style="height:633px;"><form id="'+dlgID+'_form"><fieldset><legend> Опции тестирования </legend><table>'+
'<tr><td><label>Сетевая маска области доверия:</label></td><td><select name="neighbouring"></select></td></tr>'+
'<tr><td><label>Использовать дополнительно заголовки сообщений:</label></td><td><select name="header_test"></select></td></tr>'+
'<tr><td><label>Использовать дополнительно списки по умолчанию:</label></td><td><select name="use_default_lists"></select></td></tr>'+
'</table></fieldset><br><fieldset><legend> Опции реакции на спам </legend><table>'+
'<tr><td><label>Реакция на спам по умолчанию:</label></td><td><select name="default_action"></select></td></tr>'+
'<tr><td><label>Адрес для пересылки спама:</label></td><td><select name="spam_house"></select></td></tr>'+
'<tr><td><label>Отвечать на спам сообщением:</label></td><td><input name="reject_message" type="text" value=""></td></tr>'+
'</table></fieldset><br><button type="submit" name="submit">Сохранить</button>'+
'<button name="asdef" style="margin-left:16px;">По умолчанию</button>'+
'<p></p></form></div>'        
//'</table></fieldset><br><input type="submit" name="submitButton" value="Сохранить" /><p></p></form></div>'        
        );
        $('#'+dlgID).addClass('ui-widget');
        $('#'+dlgID+' > div:first-child').css({'padding':'4.53px'});
        $('#'+dlgID+' > div:first-child + div').css({'padding':'16px'});

        $('#'+dlgID+' tr > td:first-child').css({'width':'320px','height':'32px','text-align':'right'});
        
        $('#'+dlgID+' select[name="default_action"], select[name="spam_house"]')
                .width(200).addClass('select ui-widget-content ui-corner-all');
        $('#'+dlgID+' input[name="reject_message"]')
                .width(200).addClass('text ui-widget-content ui-corner-all');
        $('#'+dlgID+' select[name="neighbouring"], select[name="use_default_lists"], select[name="header_test"]')
                .width(100).addClass('select ui-widget-content ui-corner-all');

        neighbouring = $('#'+dlgID+' select[name="neighbouring"]');
        header_test = $('#'+dlgID+' select[name="header_test"]');
        use_default_lists = $('#'+dlgID+' select[name="use_default_lists"]');

        default_action = $('#'+dlgID+' select[name="default_action"]');
        spam_house = $('#'+dlgID+' select[name="spam_house"]');
        reject_message = $('#'+dlgID+' input[name="reject_message"]');

        submit_button = $('#'+dlgID+' button[name="submit"]');
        submit_button.button({icons:{primary:'ui-icon-disk'}, disabled:true});

        default_button = $('#'+dlgID+' button[name="asdef"]');
        default_button.button({icons:{primary:'ui-icon-home'}});
        
        $('#'+dlgID+' select,input').change(function(){ 
                submit_button.button("enable"); 
        });
     
        GetFormData();
        
        $('#'+dlgID+'_form').ajaxForm({ url:'opts/write.php', type:'POST', dataType:'json', 
                    data: {oper:'putfields'}, success: submit_responce });
                    
        default_button.click( function() { SetDefaultData(); return false; } );
        
        // Добавим ToolTip
        neighbouring.attr({ systooltip: 'tooltips.php?ttname=options.neighbouring'});
        header_test.attr({ systooltip: 'tooltips.php?ttname=options.header_test'});
        use_default_lists.attr({ systooltip: 'tooltips.php?ttname=options.use_default_lists'});
        default_action.attr({ systooltip: 'tooltips.php?ttname=options.default_action'});
        spam_house.attr({ systooltip: 'tooltips.php?ttname=options.spam_house'});
        reject_message.attr({ systooltip: 'tooltips.php?ttname=options.reject_message'});
        $('#'+dlgID+'_form [systooltip]').cluetip({ 
            attribute: 'systooltip',
            cluetipClass: 'jtip',
            dropShadow: true,
            waitImage: false,
            positionBy: 'mouse',
            showTitle: false,
            escapeTitle: true,
            ajaxProcess: function(resp) {
                if( resp.errcode == '999' ) {
                    alert( resp.errmess );
                    ipdepo.go('login.php');
                    return '';
                }
                return resp.data;
            },
            ajaxSettings: {
                dataType: 'json'
            },
            onActivate: function(e) {
                return ipdepo.tooltip;
            }
        });



    }


function GetFormData() {
var dataset = { oper: "getfields" };
    
    $.post('opts/get.php', dataset, function( rsp, textStatus)
    {
        if( rsp.errcode ) {
            if( rsp.errcode == '999' ) {
                alert( "Ваша сессия закончилась по таймауту.\nПожалуйста перерегистрируйтесь." );
                ipdepo.go('login.php');
            }
            else ipdepo.alert('AlertDlg', 'Ошибка', rsp.errmess, 'ipdepo-icon-stop32');
        }
        if( $.isPlainObject(rsp.data) ) {
            data = rsp.data;
            SetCurrentData();
        }
    }, 'json');
}     


function SetCurrentData() {
var nb;
var values;
    if(data.neighbouring) {
        nb = data.neighbouring;
        values = nb.dic.values;
        for( i=0; i<values.codes.length; i++ )
            neighbouring.append('<option value="'+values.codes[i]+'">'+values.descr[i]+'</option>');
        neighbouring.val(nb.current);
        if( nb.dic.locked == '1' ) neighbouring.attr('disabled','disabled');
    }
    if(data.header_test) {
        nb = data.header_test;
        values = nb.dic.values;
        for( i=0; i<values.codes.length; i++ )
            header_test.append('<option value="'+values.codes[i]+'">'+values.descr[i]+'</option>');
        header_test.val(nb.current);
        if( nb.dic.locked == '1' ) header_test.attr('disabled','disabled');
    }
    if(data.use_default_lists) {
        nb = data.use_default_lists;
        values = nb.dic.values;
        for( i=0; i<values.codes.length; i++ )
            use_default_lists.append('<option value="'+values.codes[i]+'">'+values.descr[i]+'</option>');
        use_default_lists.val(nb.current);
        if( nb.dic.locked == '1' ) use_default_lists.attr('disabled','disabled');
    }
    if(data.default_action) {
        nb = data.default_action;
        values = nb.dic.values;
        for( i=0; i<values.codes.length; i++ )
            default_action.append('<option value="'+values.codes[i]+'">'+values.descr[i]+'</option>');
        default_action.val(nb.current);
        if( nb.dic.locked == '1' ) default_action.attr('disabled','disabled');
    }
    if(data.spam_house) {
        nb = data.spam_house;
        values = nb.dic.values;
        for( i=0; i<values.codes.length; i++ )
            spam_house.append('<option value="'+values.codes[i]+'">'+values.descr[i]+'</option>');
        spam_house.val(nb.current);
        if( nb.dic.locked == '1' ) spam_house.attr('disabled','disabled');
    }
    if(data.reject_message) {
        reject_message.val(data.reject_message.current);
        if( data.reject_message.dic.locked == '1' )
            reject_message.attr('disabled','disabled');
    }
}

function SetDefaultData() {
var dic;
    if(data.neighbouring) {
        dic = data.neighbouring.dic;
        neighbouring.val(dic.def);
        if( dic.locked == '1' ) neighbouring.attr('disabled','disabled');
    }
    if(data.header_test) {
        dic = data.header_test.dic;
        header_test.val(dic.def);
        if( dic.locked == '1' ) header_test.attr('disabled','disabled');
    }
    if(data.use_default_lists) {
        dic = data.use_default_lists.dic;
        use_default_lists.val(dic.def);
        if( dic.locked == '1' ) use_default_lists.attr('disabled','disabled');
    }
    if(data.default_action) {
        dic = data.default_action.dic;
        default_action.val(dic.def);
        if( dic.locked == '1' ) default_action.attr('disabled','disabled');
    }
    if(data.spam_house) {
        dic = data.spam_house.dic;
        spam_house.val(dic.def);
        if( dic.locked == '1' ) spam_house.attr('disabled','disabled');
    }
    if(data.reject_message) {
        dic = data.reject_message.dic;
        reject_message.val(dic.def);
        if( dic.locked == '1' ) reject_message.attr('disabled','disabled');
    }
    
    submit_button.button('enable');
}

function submit_responce(rsp) {
    submit_button.button('disable');
    if( rsp.errcode ) {
        if( rsp.errcode == '999' ) {
            alert( "Ваша сессия закончилась по таймауту.\nПожалуйста перерегистрируйтесь." );
            ipdepo.go('login.php');
        } else ipdepo.alert('AlertDlg', 'Ошибка', rsp.errmess, 'ipdepo-icon-stop32');
    }
    else ipdepo.alert('AlertDlg', 'Выполнено', 'Данные успешно сохранены', 'ipdepo-icon-info32');
}
    
};  // End of create function definition
